package com.tzbank.voice.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.tzbank.voice.utils.FileUploadUtil;



@RestController
@RequestMapping("file") 
public class fileController {

	 @RequestMapping(value="/upLoad", method={RequestMethod.POST})
	 @ResponseBody
	public String uploadFile(@RequestParam MultipartFile file) {
		
		System.out.println("=========================进入===================");
		boolean flag=true;
		String path="";
		try {
			path=FileUploadUtil.upload(file, "/home/admin");
		} catch (Exception e) {
			flag=false;
			e.printStackTrace();
		}
		System.out.println("上传的路径"+path);
		//根据类型调用不同的OCR
		String resultf=null;
		if(flag) {
			resultf="上传成功";
		}else {
			resultf="上传失败";
		}
		return resultf;
	}
	 
	 
	 @RequestMapping(value = "/downLoad", method={RequestMethod.GET})
	  public void testDownload(HttpServletResponse res,String name) {
	    //String fileName = "upload.jpg";
	    res.setHeader("content-type", "application/octet-stream");
	    res.setContentType("application/octet-stream");
	    res.setHeader("Content-Disposition", "attachment;filename="+name);
	    byte[] buff = new byte[1024];
	    BufferedInputStream bis = null;
	    OutputStream os = null;
	    try {
	      os = res.getOutputStream();
	      bis = new BufferedInputStream(new FileInputStream(new File("/home/admin/File/downLoad/"+name)));
	      int i = bis.read(buff);
	      while (i != -1) {
	        os.write(buff, 0, buff.length);
	        os.flush();
	        i = bis.read(buff);
	      }
	      System.out.println("success");
	    } catch (IOException e) {
	      e.printStackTrace();
	      System.out.println("noSuccess");
	    } finally {
	      if (bis != null) {
	        try {
	          bis.close();
	        } catch (IOException e) {
	          e.printStackTrace();
	        }
	      }
	    }
	    
	  }
	 
	 
}
